package com.heatingcharge.service.pay.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heatingcharge.common.util.ChargeModeEnum;
import com.heatingcharge.common.util.StringUtil;
import com.heatingcharge.entity.base.HeatYear;
import com.heatingcharge.entity.charge.ChargePayable;
import com.heatingcharge.entity.pay.PayInfo;
import com.heatingcharge.mapper.charge.ChargePayableMapper;
import com.heatingcharge.mapper.pay.PayInfoMapper;
import com.heatingcharge.param.ChargeInfoParam;
import com.heatingcharge.security.JwtUser;
import com.heatingcharge.security.util.SecurityUtil;
import com.heatingcharge.service.base.IHeatYearService;
import com.heatingcharge.service.charge.IChargeInfoService;
import com.heatingcharge.service.pay.IPayInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * <p>
 * 支付信息表 服务实现类
 * </p>
 *
 * @author devin.jiang
 * @since 2019-12-20
 */
@Service
public class PayInfoServiceImpl extends ServiceImpl<PayInfoMapper, PayInfo> implements IPayInfoService {

    @Autowired
    IHeatYearService heatYearService;
    @Autowired
    ChargePayableMapper chargePayableMapper;
    @Autowired
    IChargeInfoService chargeInfoService;

    @Override
    public String add(ChargeInfoParam chargeInfoParam) {
        JwtUser user = SecurityUtil.getUser();
        QueryWrapper<ChargePayable> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", chargeInfoParam.getChargePayableId());
        queryWrapper.eq("del_flag", 0);
        ChargePayable chargePayable = chargePayableMapper.selectOne(queryWrapper);
        HeatYear heatYear = heatYearService.getById(chargePayable.getHeatYearId());
        //1 向数据库中添加支付信息
        String outTradeNo = chargePayable.getHeatYearId() + "_" + chargePayable.getHouseId() + "_" + StringUtil.getOutTradeNo();
        PayInfo payInfo=new PayInfo(chargeInfoParam.getChargePayableId(),
                outTradeNo,
                chargeInfoParam.getLateFee(),
                chargeInfoParam.getDiscount(),
                chargeInfoParam.getAdditional(),
                chargeInfoParam.getReduction(),
                chargeInfoParam.getSubsidy(),
                chargeInfoParam.getActualPayable(),
                chargeInfoParam.getActual(),
                ChargeModeEnum.ALIPAY.getCode(),
                0,user.getRealName());
       baseMapper.insert(payInfo);
       return outTradeNo;
    }

    /**
     * 处理支付结果
     * @param params
     */
    @Override
    public void disposePayResult(Map<String, String> params,Integer chargeMode) {
        //订单号
        String out_trade_no=(String) params.get("out_trade_no");
        PayInfo payInfo = queryByOrderNo(out_trade_no);
        if (ObjectUtil.isNotNull(payInfo)){
            payInfo.setPayState(1);
            baseMapper.updateById(payInfo);
            //更新ChargePayable
            chargeInfoService.charge(payInfo.getPayableId(),payInfo.getActualPayable(),payInfo.getActual(),
                    payInfo.getAdditional(),payInfo.getReduction(),payInfo.getSubsidy(),payInfo.getDiscount(),
                    payInfo.getLateFee(),chargeMode,new Date(),payInfo.getPayee());

        }
    }

    @Override
    public PayInfo queryByOrderNo(String orderNo) {
        QueryWrapper<PayInfo> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);
        return baseMapper.selectOne(queryWrapper);
    }
}
